<?php

namespace App\Http\Controllers;

use App\Models\Family;
use App\Models\Project;
use App\Models\ProjectHire;
use App\Models\Team;
use Carbon\Carbon;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class FamilyController extends Controller
{


    public function index(Request $request) {
        $perPage = $request->get('perPage', 10);
        $query = Family::with('volunteer');

        $user = $request->user();

        if ($user->type == 3) {
            $query->whereRaw("user_id in (select id from volunteer where family_id > 0 and org_id={$user->org_id})");
        }  else if ($user->type == 4 && $request->has('org_id')) {
            $query->whereRaw("user_id in (select id from volunteer where family_id > 0 and org_id={$request->has('org_id')})");
        }

        if ($request->get('keyword')) {
            $query->whereHas('volunteer',  function ($query) use ($request) {

                $query->where('real_name', 'like', "%{$request->get('keyword')}%");
            });
        }

        if($request->get('orderBy') && $request->get('orderDir'))
        {
            $query->orderBy($request->get('orderBy'), $request->get('orderDir'));
        }

        //列表按id倒排
        $query->orderBy('id', 'desc');

        $info = $query->paginate($perPage)->toArray();

        Log::info($info);

        return $this->returnJSON([
            'count' => $info['total'],
            'rows' => $info['data']
        ]);
    }


}
